package com.chromatic.competition.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DatabaseHelper {
    private static final String DB_URL = "jdbc:mysql://43.155.141.14/sk_user_data?useSSL=false";
    private static final String DB_USER = "SKOfficial";
    private static final String DB_PASSWORD = "mysqlSK";

    /**
     * 连接数据库
     *
     * @return 数据库连接对象
     * @throws SQLException 如果连接失败抛出 SQLException 异常
     */
    public static Connection connect() throws SQLException {
        return DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
    }

    /**
     * 关闭数据库连接
     *
     * @param connection 数据库连接对象
     */
    public static void close(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭 Statement 对象
     *
     * @param statement Statement 对象
     */
    public static void close(Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭 ResultSet 对象
     *
     * @param resultSet ResultSet 对象
     */
    public static void close(ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}